#!/usr/bin/env python

import os, sys
import mad
import glob
import getpass
import gdata
import gdata.youtube.service
import math

developer_key = "AI39si7BsacKDuEdC91xBvNoov44fm_hGvr6EQAVX9LktyhnZhAilTMyhQ9OAAhkkebVs_BewtHpjkjzsrNz7KETSLr6DJhWTQ"
clientid = "emlynoregan"

def executecmd(cmdstring):
	print cmdstring
	os.system(cmdstring)
	
 
if (len(sys.argv) > 1):
	path = sys.argv[1]
else:
	path = '.'


for infile in glob.glob( os.path.join(path, '*raw.mpg') ):	
	# set filename
	print ("infile=" + infile)
	workingfileprefix = infile[0:len(infile)-7]
	print("workingfileprefix=" + workingfileprefix)

	# delete files which will be generated
	if (os.path.exists('%sraw.mp3' % (workingfileprefix))):
		executecmd('rm %sraw.mp3' % (workingfileprefix))

	# extract audio to *raw.mp3
	executecmd(('/home/emlyn/dev/vidworking/extractaudio.py %sraw.mpg %sraw.mp3') % (workingfileprefix, workingfileprefix))	

for infile in glob.glob( os.path.join(path, '*raw.mp3') ):	
	# set filename
	print ("infile=" + infile)
	workingfileprefix = infile[0:len(infile)-7]
	print("workingfileprefix=" + workingfileprefix)

	#determine length
	mp3name = workingfileprefix + "raw.mp3";
	mf = mad.MadFile(mp3name)
	track_length_in_milliseconds = mf.total_time()

	number_of_10min_chunks = math.ceil(track_length_in_milliseconds / 600000.0)
	print ("track_length_in_milliseconds=%u, number_of_10min_chunks=%s" % (track_length_in_milliseconds, number_of_10min_chunks))

		
print ("Done")

